package cn.test.learnkotlin.v1

import android.view.View

private class LExpression {

    /**
     * 对象表达式
     */
    object ClickListener : View.OnClickListener {
        val foo = 1
        override fun onClick(v: View?) {
        }
    }

    open class A(x: Int) {
        public open val y: Int = x
    }

    interface B {}

    /**
     * 对象可以继承于某个基类，或者实现其他接口
     */
    val ab: A = object : A(1), B {
        override val y: Int
            get() = 15
    }

    /**
     * 通过对象表达式可以越过类的定义直接得到一个对象
     */
    fun fooObject() {
        val site = object {
            val name = "runoob"
            val url = "www.runoob.com"
        }
        println(site.name)
        println(site.url)
    }

    /**
     * Scope1
     */
    private val privateSite = object {
        val name = "runoob"
        val url = "www.runoob.com"
    }

    val publicSite = object {
        val name = "runoob"
        val url = "www.runoob.com"
    }

    object Site : Any() {
        const val name = "runoob"
        const val url = "www.runoob.com"
    }

    fun fooScope1() {
        ClickListener.foo
        Site.name           //可以访问
        Site.url            //可以访问
        privateSite.name    //可以访问
        privateSite.url     //可以访问
        //publicSite.name   //无法访问
        //publicSite.url    //无法访问
    }

    class Site1 {
        var name = "菜鸟教程"

        object Desktop {
            var url = "www.runoob.com"
            fun showName() {
                println("desk legs ${Site.name}")
            }
        }

        var desktop = object {
            var url = "www.runoob.com"
            fun showName() {
                println("desk legs $name")
            }
        }
    }

    fun fooDeclareObject() {
        val site = Site1()
        site.name
        site.desktop
        Site1.Desktop.url
    }

    /**
     * 伴生对象
     */
    class MyClass {
        companion object {
            fun create() = MyClass()
        }
    }

    fun fooCompanionObject(){
        MyClass.create()
    }

    fun main() {

    }
}

fun main() {
    LExpression().main()
}